package net.mhelvens.deltaprofiles;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

abstract public class BaseLogicalCondition extends Condition {
	private static final String TAG = BaseLogicalCondition.class.getSimpleName();
	
	final protected View getLabelView(String str) { return getLabelView(str, 0, 0); }
	final protected View getLabelView(String str, int leftPaddingId, int rightPaddingId) {
		TextView result = getLabelView();
		result.setText(str);
		addPadding(result, leftPaddingId, rightPaddingId);
		
		return result;
	}

	final protected View getLabelView(int resId) { return getLabelView(resId, 0, 0); }
	final protected View getLabelView(int resId, int leftPaddingId, int paddRight) {
		TextView result = getLabelView();
		result.setText(resId);
		addPadding(result, leftPaddingId, paddRight);
		
		return result;
	}

	final protected View getPotentialLabelView(int resId) { return getLabelView(resId, 0, 0); }
	final protected View getPotentialLabelView(int resId, int leftPaddingId, int paddRight) {
		TextView result = getPotentialLabelView();
		result.setText(resId);
		addPadding(result, leftPaddingId, paddRight);
		
		return result;
	}
	
	private void addPadding(View v, int leftPaddingId, int rightPaddingId) {
		v.setPadding(
			( leftPaddingId == 0 ? 0 : context().getResources().getDimensionPixelOffset( leftPaddingId)), v.getPaddingTop(),
			(rightPaddingId == 0 ? 0 : context().getResources().getDimensionPixelOffset(rightPaddingId)), v.getPaddingBottom()
		);
	}
	
	private TextView getLabelView() {
		return (TextView) ((LayoutInflater)context().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.formula_label, null);
	}
	
	private TextView getPotentialLabelView() {
		TextView result = (TextView) ((LayoutInflater)context().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.formula_label, null);
		result.setTextColor(context().getResources().getColor(R.color.grey));
		result.setTypeface(result.getTypeface(), Typeface.ITALIC);
		result.setTextSize(10);
		result.setPadding(0, context().getResources().getDimensionPixelOffset(R.dimen.push_potential_label_down), 0, 0);
		return result;
	}
	
	protected interface OnDropListener {
		void onDrop(View v, DragEvent event);
	}

	final protected View dropTarget(final OnChanged oc, final int index) {
		return dropTarget(new OnDropListener() {
			public void onDrop(View v, DragEvent event) {
				try {
					Condition.create(event.getClipData().getItemAt(0).getUri().toString(), BaseLogicalCondition.this, index);
					oc.notifyView();
				} catch (Exception e) {
					Log.e(TAG, "Error instantiating new condition", e);
					e.printStackTrace();
					// TODO: proper error handling
				}
			}
		}, index);
	}
	
	final protected View dropTarget(final OnDropListener odl, int index) {
		final ImageView result = (ImageView) ((LayoutInflater)context().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drop_target, null);
		result.setOnDragListener(new View.OnDragListener() {
			public boolean onDrag(View v, DragEvent event) {
				switch(event.getAction()) {
					
					case DragEvent.ACTION_DRAG_STARTED: {
						return true;
					}
					
					case DragEvent.ACTION_DRAG_ENTERED: {
						result.setImageDrawable(context().getResources().getDrawable(R.drawable.drop_target_open));
						return true;
					}
					
					case DragEvent.ACTION_DRAG_EXITED: {
						result.setImageDrawable(context().getResources().getDrawable(R.drawable.drop_target_closed));
						return true;
					}
					
					case DragEvent.ACTION_DROP: {
						odl.onDrop(v, event);
						return true;
					}
					
				}
				
				return false;
			}
		});
		
		return result;
	}
	
	
}
